package com.ioidea.holland.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.provider.Settings
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.ioidea.base.util.MyLogger
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.impl.LoadingPopupView
import java.util.LinkedList

@SuppressLint("StaticFieldLeak")
var loadingPopup: LoadingPopupView? = null

fun AppCompatActivity.showLoading(message: String = "加载中") {
    if (!this.isFinishing) {
        if (loadingPopup == null) {
            loadingPopup = XPopup.Builder(this)
                .dismissOnBackPressed(true)
                .dismissOnTouchOutside(false)
//                .isLightNavigationBar(true)
                .asLoading(message, LoadingPopupView.Style.Spinner)
                as LoadingPopupView
            loadingPopup!!.show()
        } else {
            loadingPopup!!.setStyle(LoadingPopupView.Style.Spinner)
            loadingPopup!!.setTitle(message)
            loadingPopup!!.show()
        }
    }
}



fun Fragment.showLoading(message: String = "加载中") {
    if (!this.isRemoving) {
        if (loadingPopup == null) {
            loadingPopup = XPopup.Builder(requireContext())
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .isLightNavigationBar(true)
                .asLoading(message, LoadingPopupView.Style.Spinner)
                    as LoadingPopupView
            loadingPopup!!.show()
        } else {
            loadingPopup!!.setStyle(LoadingPopupView.Style.Spinner)
            loadingPopup!!.setTitle(message)
            loadingPopup!!.show()
        }
    }
}


fun hideLoading() {
    loadingPopup?.dismiss()
    loadingPopup = null
}



fun AppCompatActivity.showPermissionDialog(
    context: Context,
    permissions: String
) {
    AlertDialog.Builder(context)
        .setCancelable(false)
        .setTitle("提示")
        .setMessage(permissions)
        .setPositiveButton("设置",
            { dialog, which -> setPermission() })
        .setNegativeButton(com.ioidea.base.R.string.cancel, null).show()
}

fun AppCompatActivity.handPermission(
    permissionHintMap: HashMap<String, String?>,
    permissions: Array<String>,
    grantResults: IntArray,
    callBack:(isAllGrant:Boolean)->Unit
) {

    val failPermissions: MutableList<String> = LinkedList()
    for (i in grantResults.indices) {
        if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
            failPermissions.add(permissions[i])
        }
    }
    if (failPermissions.isNotEmpty()) {
        MyLogger.hLog().e("权限未赋予")
        val sb = StringBuilder()
        for (permission in failPermissions) {
            sb.append(permissionHintMap[permission]).append("、")
        }
        sb.deleteCharAt(sb.length - 1)
        val hint = "未授予必要权限: " +
                sb.toString() +
                "，请前往设置页面开启权限"
        MyLogger.hLog().e(hint)

        showPermissionDialog(this,hint)
    }else if (permissions.isNotEmpty()){
        MyLogger.hLog().e("权限都赋予了")
        callBack(true)
    }

}
fun AppCompatActivity. setPermission() {
    val intent = Intent()
    intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
    val uri = Uri.fromParts("package", packageName, null)
    intent.data = uri
    startActivity(intent)
//    System.exit(0)
}